package com.link.linkupjavaweb.controller.user;

import com.link.linkupjavaweb.common.Constants;
import com.link.linkupjavaweb.common.Result;
import com.link.linkupjavaweb.entity.User;
import com.link.linkupjavaweb.service.GameService;
import com.link.linkupjavaweb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户数据统计Controller
 */
@Controller
@RequestMapping("/user/stats")
public class UserStatsController {
    
    @Autowired
    private GameService gameService;
    
    @Autowired
    private UserService userService;
    
    /**
     * 个人数据统计页面
     */
    @GetMapping
    public String statsPage(HttpSession session, Model model) {
        User user = (User) session.getAttribute(Constants.SESSION_USER);
        if (user == null) {
            return "redirect:/user/login";
        }
        
        // 刷新用户信息（获取最新数据）
        User latestUser = userService.getUserById(user.getId());
        model.addAttribute("user", latestUser);
        
        return "user/stats/index";
    }
    
    /**
     * 获取用户详细统计数据（API）
     */
    @GetMapping("/data")
    @ResponseBody
    public Result<Map<String, Object>> getStatsData(HttpSession session) {
        try {
            User user = (User) session.getAttribute(Constants.SESSION_USER);
            if (user == null) {
                return Result.error("请先登录");
            }
            
            // 获取用户最新信息
            User latestUser = userService.getUserById(user.getId());
            
            // 获取游戏统计数据
            Object gameStats = gameService.getUserGameStats(user.getId());
            
            // 构建返回数据
            Map<String, Object> data = new HashMap<>();
            data.put("user", latestUser);
            data.put("gameStats", gameStats);
            
            // 计算胜率
            if (latestUser.getGameCount() > 0) {
                double winRate = (double) latestUser.getWinCount() / latestUser.getGameCount() * 100;
                data.put("winRate", String.format("%.1f", winRate));
            } else {
                data.put("winRate", "0.0");
            }
            
            // 计算平均分
            if (latestUser.getGameCount() > 0) {
                double avgScore = (double) latestUser.getTotalScore() / latestUser.getGameCount();
                data.put("avgScore", String.format("%.0f", avgScore));
            } else {
                data.put("avgScore", "0");
            }
            
            return Result.success(data);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取统计数据失败：" + e.getMessage());
        }
    }
}
